package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysPositionsAvailableMapper;
import com.ruoyi.system.domain.SysPositionsAvailable;
import com.ruoyi.system.service.ISysPositionsAvailableService;

import javax.validation.Validator;

/**
 * 招聘岗位信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-16
 */
@Service
public class SysPositionsAvailableServiceImpl implements ISysPositionsAvailableService 
{

    private static final Logger log = LoggerFactory.getLogger(SysPositionsAvailableServiceImpl.class);
    @Autowired
    private SysPositionsAvailableMapper sysPositionsAvailableMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询招聘岗位信息
     * 
     * @param positionCoded 招聘岗位信息主键
     * @return 招聘岗位信息
     */
    @Override
    public SysPositionsAvailable selectSysPositionsAvailableByPositionCode(String positionCoded)
    {
        return sysPositionsAvailableMapper.selectSysPositionsAvailableByPositionCode(positionCoded);
    }

    /**
     * 查询招聘岗位信息列表
     * 
     * @param sysPositionsAvailable 招聘岗位信息
     * @return 招聘岗位信息
     */
    @Override
    public List<SysPositionsAvailable> selectSysPositionsAvailableList(SysPositionsAvailable sysPositionsAvailable)
    {
        return sysPositionsAvailableMapper.selectSysPositionsAvailableList(sysPositionsAvailable);
    }

    /**
     * 新增招聘岗位信息
     * 
     * @param sysPositionsAvailable 招聘岗位信息
     * @return 结果
     */
    @Override
    public int insertSysPositionsAvailable(SysPositionsAvailable sysPositionsAvailable)
    {
        sysPositionsAvailable.setCreateTime(DateUtils.getNowDate());
        return sysPositionsAvailableMapper.insertSysPositionsAvailable(sysPositionsAvailable);
    }

    /**
     * 修改招聘岗位信息
     * 
     * @param sysPositionsAvailable 招聘岗位信息
     * @return 结果
     */
    @Override
    public int updateSysPositionsAvailable(SysPositionsAvailable sysPositionsAvailable)
    {
        sysPositionsAvailable.setUpdateTime(DateUtils.getNowDate());
        return sysPositionsAvailableMapper.updateSysPositionsAvailable(sysPositionsAvailable);
    }

    /**
     * 批量删除招聘岗位信息
     * 
     * @param positionIds 需要删除的招聘岗位信息主键
     * @return 结果
     */
    @Override
    public int deleteSysPositionsAvailableByPositionIds(Long[] positionIds)
    {
        return sysPositionsAvailableMapper.deleteSysPositionsAvailableByPositionIds(positionIds);
    }

    /**
     * 删除招聘岗位信息信息
     * 
     * @param positionId 招聘岗位信息主键
     * @return 结果
     */
    @Override
    public int deleteSysPositionsAvailableByPositionId(Long positionId)
    {
        return sysPositionsAvailableMapper.deleteSysPositionsAvailableByPositionId(positionId);
    }

    /**
 * 导入招聘岗位数据
 *
 * @param availableList 招聘岗位数据列表
 * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
 * @param operName 操作用户
 * @return 结果
 */
@Override
public String importPositionsAvailable(List<SysPositionsAvailable> availableList, Boolean isUpdateSupport, String operName)
{
    if (StringUtils.isNull(availableList) || availableList.size() == 0)
    {
        throw new ServiceException("导入招聘岗位数据不能为空！");
    }

    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();

    for (SysPositionsAvailable available : availableList)
    {
        try
        {
            // 验证是否存在该岗位（通过岗位编码或岗位名称判断）
            SysPositionsAvailable existingAvailable = sysPositionsAvailableMapper.selectSysPositionsAvailableByPositionCode(available.getPositionCode());

            if (StringUtils.isNull(existingAvailable))
            {
                // 校验数据合法性
                BeanValidators.validateWithException(validator, available);

                // 插入新岗位
                available.setCreateBy(operName);
                sysPositionsAvailableMapper.insertSysPositionsAvailable(available);
                successNum++;
                successMsg.append("<br/>" + successNum + "、岗位编码 " + available.getPositionCode() + " 导入成功");
            }
            else if (isUpdateSupport)
            {
                // 如果允许更新，则更新已有记录
                BeanValidators.validateWithException(validator, available);
                available.setPositionId(existingAvailable.getPositionId()); // 保留原有ID
                available.setUpdateBy(operName);
                sysPositionsAvailableMapper.updateSysPositionsAvailable(available);
                successNum++;
                successMsg.append("<br/>" + successNum + "、岗位编码 " + available.getPositionCode() + " 更新成功");
            }
            else
            {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、岗位编码 " + available.getPositionCode() + " 已存在");
            }
        }
        catch (Exception e)
        {
            failureNum++;
            String msg = "<br/>" + failureNum + "、岗位编码 " + available.getPositionCode() + " 导入失败：";
            failureMsg.append(msg + e.getMessage());
            log.error(msg, e);
        }
    }

    if (failureNum > 0)
    {
        failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
        throw new ServiceException(failureMsg.toString());
    }
    else
    {
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
    }

    return successMsg.toString();
}

}
